package aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class Client {

    private AsynchronousSocketChannel client;

    public Client(String host, int port) throws IOException, InterruptedException, ExecutionException {

        this.client = AsynchronousSocketChannel.open();
        Future<?> future = client.connect(new InetSocketAddress(host, port));
        future.get();
    }

    public void write(byte b) {

        ByteBuffer buffer = ByteBuffer.allocate(32);
        buffer.put(b);
        buffer.flip();
        client.write(buffer);
    }

    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {

        Client client = new Client("127.0.0.1", 8888);
        client.write((byte) 11);
    }
}
